<?php

namespace App\Http\Controllers\Commit;

use App\Repository\Commit\PositionRepository;
use App\Repository\Commit\RoleRepository;
use App\Repository\SystemRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;

/**
 * @OA\Tag(
 *     name="后台菜单模块",
 *     description="菜单模块",
 * )
 */
class PositionController extends BaseController
{
  public function __construct(PositionRepository $PositionRepository, SystemRepository $SystemRepository, RoleRepository $RoleRepository)
  {
    $this->repoist = $PositionRepository;
    $this->system = $SystemRepository;
    $this->role = $RoleRepository;
  }
  public function store(Request $request)
  {

    $request->offsetSet('position_code', $request->get('positionCode'));
    $request->offsetSet('position_name', $request->get('positionName'));
    $request->offsetSet('position_sort', $request->get('positionSort'));
    $request->offsetSet('status', $request->get('status'));
    $request->offsetSet('role_id', $request->get('roleId'));
    $request->offsetSet('remark', $request->get('remark'));
    return $this->repoist->store($request);
  }
  public function index(Request $request)
  {
    $request = usfirstArr($request->all());
    $data =  $this->repoist->index($request);
    return $data;
  }
  public function save(Request $request)
  {
    if (!$request->get('positionId')) {
      return $this->returnMsg(1, '请传递id参数');
    }
    $where = ['position_id' => $request->get('positionId')];
    $request->offsetSet('position_code', $request->get('positionCode'));
    $request->offsetSet('position_name', $request->get('positionName'));
    $request->offsetSet('position_sort', $request->get('positionSort'));
    $request->offsetSet('status', $request->get('status'));
    $request->offsetSet('remark', $request->get('remark'));
    $request->offsetSet('role_id', $request->get('roleId'));
    return $this->repoist->save($request, $request->get('positionId'));
  }
  public function  show(Request $request, $id)
  {
    $where = [
      'position_id' => $id,
      'shop_id' => $request->user->shop_id
    ];
    //查询一条数据
    $return = $this->repoist->getOne($where);
    if (!empty($return)) {
      $return = usfirstArr($return);
      if (!empty($return['roleId'])) {

        $res = explode(',', $return['roleId']);
        $return['roleId'] = [];
        foreach ($res as $key => $v) {
          $return['roleId'][] = (int)$v;
        }
      }
    }
    return $this->returnMsg(200, '查询成功', $return);
  }

  public function listss(Request $request)
  {
    $where = [];
    if (!empty($request->get('name'))) {
      $where['where'] = ['menu_tag' => $request->get('name')];
    }

    $return = $this->repoist->lists($where);
    return $return;
  }


  public function destroy(Request $request, $id)
  {
    if (!$id) {
      return $this->returnMsg(1, '请传递参数');
    }
    $where = [
      'position_id' => $id,
      'shop_id' => $request->user->shop_id
    ];
    return $this->repoist->del($where);
  }
}
